//app.js
var util = require('/utils/request.js')
App({
  globalData: {
    rex : /^1\d{10}$/,
    in_shopping_cart: {
      count: 0,
      allPrice: 0,
      list:[]
    },
  
    out_shopping_cart: {
      count: 0,
      allPrice: 0,
      list: []
    },
    add_shopping_cart: {
      count: 0,
      allPrice: 0,
      list:[]
    },
    order_seat_sn: '',
    sid: '',
    version:'1.1.11.5',
    userInfo: null,
    eating_status: null,
    key: 'a2fbe4c8892caa5dfaf42ac248b6871c',
    //新购物车
    shoppingCart: [],
    //服务端解码
    server_scan_parse: 0
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    this.getUserInfo()
  },
  checkOrderFinish: function(){

  },
  http: require('/utils/request.js'),
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function (out_res){
          if (out_res.code) {
            //发起网络请求
            wx.getUserInfo({
              success: function (resInfo) {
                that.http.request({
                  url: '/api/v1/consumer/login',
                  data: {
                    iv: resInfo.iv,
                    code: out_res.code,
                    ed: resInfo.encryptedData,
                    signature: resInfo.signature
                  },
                  success: function (resLogin) {
                    console.log(resLogin);
                    var serverResult = resLogin.data;
                    that.globalData.sid = serverResult.data.sid
                    wx.setStorageSync('sid', that.globalData.sid)
                  }
                })
              }
            })
          } else {
            console.log('获取用户登录态失败！' + out_res.errMsg)
          }

        }
      })
    }
  },
  /**
   * 初始化全局点餐购物车（主要目的是创建一个点餐购物车的数据结构）
   */
  inShoppingCartModify: function (list) {
    for (var i = 0; i < list.length; i++) {
      this.globalData.in_shopping_cart.list.push({ name: list[i].name, price: list[i].price, num: 0, id: list[i].id})
    }
    console.log(this.globalData.in_shopping_cart)
  },
  /**
   * 初始化全局外卖购物车（主要目的是创建一个外卖购物车的数据结构）
   */ 
  outShoppingCartModify: function (list) {
    for (var i = 0; i < list.length; i++) {
      this.globalData.out_shopping_cart.list.push({ name: list[i].name, price: list[i].price, num: 0, id: list[i].id })
    }
  },
  /**
   * 初始化全局加菜购物车（主要目的是创建一个加菜购物车的数据结构）
   */
  addShoppingCartModify: function (list) {
    for (var i = 0; i < list.length; i++) {
      this.globalData.add_shopping_cart.list.push({ name: list[i].name, price: list[i].price, num: 0, id: list[i].id })
    }
    console.log(this.globalData.add_shopping_cart)
  },

/***************************************** */
  /** 
   * 增加点餐购物车商品
   */
  numAddInShoppingCart: function (index){
    var indexInt = parseInt(index)
    var numadd = parseInt(this.globalData.in_shopping_cart.list[indexInt].num)+1
    this.globalData.in_shopping_cart.list.splice(indexInt, 1, { 'name': this.globalData.in_shopping_cart.list[index].name,'price': this.globalData.in_shopping_cart.list[index].price, 'num': numadd, 'id': this.globalData.in_shopping_cart.list[index].id }
)
    console.log(this.globalData.in_shopping_cart.list)
    //计算总价和总量
    var allPrice = 0
    var count = 0
    for (var i = 0; i < this.globalData.in_shopping_cart.list.length; i++) {
      allPrice += parseInt(this.globalData.in_shopping_cart.list[i].num) * parseInt(this.globalData.in_shopping_cart.list[i].price)
      count += parseInt(this.globalData.in_shopping_cart.list[i].num)
    }
    this.globalData.in_shopping_cart.allPrice = allPrice
    this.globalData.in_shopping_cart.count = count
  },
  /** 
   * 减少点餐购物车商品
   */
  numMinusInShoppingCart: function (index) {
    var indexInt = parseInt(index)
    if (this.globalData.in_shopping_cart.list[indexInt].num >= 1) {
      var numadd = parseInt(this.globalData.in_shopping_cart.list[indexInt].num) - 1
      this.globalData.in_shopping_cart.list.splice(indexInt, 1, { 'name': this.globalData.in_shopping_cart.list[index].name, 'price': this.globalData.in_shopping_cart.list[index].price, 'num': numadd, 'id': this.globalData.in_shopping_cart.list[index].id }
      )
      console.log(this.globalData.in_shopping_cart.list)
      //计算总价和总量
      var allPrice = 0
      var count = 0
      for (var i = 0; i < this.globalData.in_shopping_cart.list.length; i++) {
        allPrice += parseInt(this.globalData.in_shopping_cart.list[i].num) * parseInt(this.globalData.in_shopping_cart.list[i]
        .price)
        count += parseInt(this.globalData.in_shopping_cart.list[i].num)
      }
      this.globalData.in_shopping_cart.allPrice = allPrice
      this.globalData.in_shopping_cart.count = count
    }
  },

/*************************************************** */
  /** 
   * 增加外卖购物车商品
   */
  numAddOutShoppingCart: function (index) {
    var indexInt = parseInt(index)
    var numadd = parseInt(this.globalData.out_shopping_cart.list[indexInt].num) + 1
    this.globalData.out_shopping_cart.list.splice(indexInt, 1, { 'name': this.globalData.out_shopping_cart.list[index].name, 'price': this.globalData.out_shopping_cart.list[index].price, 'num': numadd, 'id': this.globalData.out_shopping_cart.list[index].id }
    )
    console.log(this.globalData.out_shopping_cart.list)
    //计算总价和总量
    var allPrice = 0
    var count = 0
    for (var i = 0; i < this.globalData.out_shopping_cart.list.length; i++) {
      allPrice += parseInt(this.globalData.out_shopping_cart.list[i].num) * parseInt(this.globalData.out_shopping_cart.list[i].price)
      count += parseInt(this.globalData.out_shopping_cart.list[i].num)
    }
    this.globalData.out_shopping_cart.allPrice = allPrice
    this.globalData.out_shopping_cart.count = count
  },
  /** 
   * 减少外卖购物车商品
   */
  numMinusOutShoppingCart: function (index) {
    var indexInt = parseInt(index)
    if (this.globalData.out_shopping_cart.list[indexInt].num >= 1) {
      var numadd = parseInt(this.globalData.out_shopping_cart.list[indexInt].num) - 1
      this.globalData.out_shopping_cart.list.splice(indexInt, 1, { 'name': this.globalData.out_shopping_cart.list[index].name, 'price': this.globalData.out_shopping_cart.list[index].price, 'num': numadd, 'id': this.globalData.out_shopping_cart.list[index].id }
      )
      console.log(this.globalData.out_shopping_cart.list)
      //计算总价和总量
      var allPrice = 0
      var count = 0
      for (var i = 0; i < this.globalData.out_shopping_cart.list.length; i++) {
        allPrice += parseInt(this.globalData.out_shopping_cart.list[i].num) * parseInt(this.globalData.out_shopping_cart.list[i]
          .price)
        count += parseInt(this.globalData.out_shopping_cart.list[i].num)
      }
      this.globalData.out_shopping_cart.allPrice = allPrice
      this.globalData.out_shopping_cart.count = count
    }
  },

  /*************************************************** */
  /** 
   * 增加加菜购物车商品
   */
  numAddAddShoppingCart: function (index) {
    var indexInt = parseInt(index)
    var numadd = parseInt(this.globalData.add_shopping_cart.list[indexInt].num) + 1
    this.globalData.add_shopping_cart.list.splice(indexInt, 1, { 'name': this.globalData.add_shopping_cart.list[index].name, 'price': this.globalData.add_shopping_cart.list[index].price, 'num': numadd, 'id': this.globalData.add_shopping_cart.list[index].id }
    )
    console.log(this.globalData.add_shopping_cart.list)
    //计算总价和总量
    var allPrice = 0
    var count = 0
    for (var i = 0; i < this.globalData.add_shopping_cart.list.length; i++) {
      allPrice += parseInt(this.globalData.add_shopping_cart.list[i].num) * parseInt(this.globalData.add_shopping_cart.list[i].price)
      count += parseInt(this.globalData.add_shopping_cart.list[i].num)
    }
    this.globalData.add_shopping_cart.allPrice = allPrice
    this.globalData.add_shopping_cart.count = count
  },
  /** 
   * 减少加菜购物车商品
   */
  numMinusAddShoppingCart: function (index) {
    var indexInt = parseInt(index)
    if (this.globalData.add_shopping_cart.list[indexInt].num >= 1) {
      var numadd = parseInt(this.globalData.add_shopping_cart.list[indexInt].num) - 1
      this.globalData.add_shopping_cart.list.splice(indexInt, 1, { 'name': this.globalData.add_shopping_cart.list[index].name, 'price': this.globalData.add_shopping_cart.list[index].price, 'num': numadd, 'id': this.globalData.add_shopping_cart.list[index].id }
      )
      console.log(this.globalData.add_shopping_cart.list)
      //计算总价和总量
      var allPrice = 0
      var count = 0
      for (var i = 0; i < this.globalData.add_shopping_cart.list.length; i++) {
        allPrice += parseInt(this.globalData.add_shopping_cart.list[i].num) * parseInt(this.globalData.add_shopping_cart.list[i]
          .price)
        count += parseInt(this.globalData.add_shopping_cart.list[i].num)
      }
      this.globalData.add_shopping_cart.allPrice = allPrice
      this.globalData.add_shopping_cart.count = count
    }
  },

  /**
   * 往购物车中添加商品
   */
  addProductToCart: function (data){
    //data数据格式：mid: {inShoppingCart: {count: 0, allPrice: 0, list:[]},outShoppingCart: {},addShoppingCart: {}}
    this.globalData.shoppingCart.push(data)
  },

  /**
   * 修改购物车信息
   */
  changeShoppingCart: function (mid, category, data) {
    var tmp_shoppingCart = this.globalData.shoppingCart
    switch (category) {
      case '点餐':
        var midExist = false
        if (tmp_shoppingCart.length > 0) {
          for (var i = 0; i < tmp_shoppingCart.length; i++) {
            if (mid == tmp_shoppingCart[i].mid) {
              tmp_shoppingCart[i].data.inShoppingCart = data
              midExist = true
            }
          }
        }
        if (tmp_shoppingCart.length <= 0 || !midExist) {
          tmp_shoppingCart.push({ 'mid': mid, 'data': { 'inShoppingCart': data, 'outShoppingCart': {}, 'addShoppingCart': {}} })
        }
      break;
      case '外卖':
        var midExist = false
        if (tmp_shoppingCart.length > 0) {
          for (var i = 0; i < tmp_shoppingCart.length; i++) {
            if (mid == tmp_shoppingCart[i].mid) {
              tmp_shoppingCart[i].data.outShoppingCart = data
              midExist = true
            }
          }
          console.log(tmp_shoppingCart)
        }
        if (tmp_shoppingCart.length <= 0 || !midExist) {
          tmp_shoppingCart.push({ 'mid': mid, 'data': { 'inShoppingCart': {}, 'outShoppingCart': data, 'addShoppingCart': {} } })
        }
      break;
      case '加菜':
        var midExist = false
        if (tmp_shoppingCart.length > 0) {
          for (var i = 0; i < tmp_shoppingCart.length; i++) {
            if (mid == tmp_shoppingCart[i].mid) {
              tmp_shoppingCart[i].data.addShoppingCart = data
              midExist = true
            }
          }
        }
        if (tmp_shoppingCart.length <= 0 || !midExist) {
          tmp_shoppingCart.push({ 'mid': mid, 'data': { 'inShoppingCart': {}, 'outShoppingCart': {}, 'addShoppingCart': data } })
        }
      break;
      default:
      break;
    }
    this.globalData.shoppingCart = tmp_shoppingCart
    console.log(this.globalData.shoppingCart)
  },

  /**
   * 清除购物车指定数据
   */
   cleanShoppingCart: function (mid, category) {
     for (var i = 0; i < this.globalData.shoppingCart.length; i++) {
       if (this.globalData.shoppingCart[i].mid == mid) {
         switch (category) {
            case '点餐':
              this.globalData.shoppingCart[i].data.inShoppingCart = {}
              break;
            case '外卖':
             this.globalData.shoppingCart[i].data.outShoppingCart = {}
              break;
            case '加菜':
             this.globalData.shoppingCart[i].data.addShoppingCart = {}
              break;
            default:
              break;
         }
       }
     }
     console.log(this.globalData.shoppingCart)
   },
  
})
